/**
 * 业务字典 编辑页
 *
 * 作者 张雨生
 * 日期 2020-05-22 15:28:25
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.$,
        febs = layui.febs,
        form = layui.form,
        $view = $('#dict-edit'),
        validate = layui.validate;

    form.verify(validate);
    form.render();
    initFormValue();

    function initFormValue() {
        if (dictData) {
            $view.find('input[name="dictCode"]').attr("disabled","disabled")//将input元素设置为disabled
            form.val("dict-edit-form", {
                "dictId": dictData.dictId,
                "status":dictData.status,
                "dictCode": dictData.dictCode,
                "dictName": dictData.dictName,
                "remark": dictData.remark,
            });
            $.each(dictData.dictItems,function (i,v) {
                var item = '<div class="layui-row  layui-col-space10">\n' +
                    '                    <div class=" layui-col-md5">\n' +
                    '                        <label class="layui-form-label jhg-form-item-require">项目编码：</label>\n' +
                    '                        <div class="layui-input-block">\n' +
                    '                            <input type="text" autocomplete="off" class="layui-input"\n' +
                    '                                   maxlength="20" value="'+v.itemCode+'" placeholder="请输入项目编码" disabled >\n' +
                    '                        </div>\n' +
                    '                    </div>\n' +
                    '                    <div class=" layui-col-md5">\n' +
                    '                        <label class="layui-form-label jhg-form-item-require">项目名称：</label>\n' +
                    '                        <div class="layui-input-block">\n' +
                    '                            <input type="text" autocomplete="off" class="layui-input"\n' +
                    '                                   maxlength="20" value="'+v.itemName+'"  placeholder="请输入项目名称" >\n' +
                    '                        </div>\n' +
                    '                    </div>\n' +
                    '                    <div class=" layui-col-md1" style="text-align: center;">\n' +
                    '                            <input type="checkbox" lay-skin="switch" ';
                    if(v.itemStatus == '1'){
                        item += ' checked="checked"';
                    }
                    item +=' lay-text="启用|禁用"></div>\n' +
                    '                    <div class=" layui-col-md1">\n' +
                    '                       <div class="layui-input-block-html deleteItem"><em class="layui-icon febs-red" style="cursor: pointer;" title="移除">&#xe7f9;</em></div>\n' +
                    '                    </div>\n' +
                    '                </div>';
                $view.find("#itemContent").append(item);
            })
            form.render();
        }
    }

    form.on('submit(dict-edit-form-submit)', function (data) {
        addOrUpdate(data.field);

        return false;
    });

    function addOrUpdate(data) {
        var itemArr = [];
        $("#itemContent").find('.layui-row').each(function (i,v) {
            var _code = $($(this).children("div").get(0)).find("input").val();
            var _name = $($(this).children("div").get(1)).find("input").val();
            var _itemStatus = $($(this).children("div").get(2)).find("input").prop('checked')==true?'1':'0';
            var _sort = i+1;
            var item = new Item(null,data.dictCode,_code,_name,_itemStatus,_sort);
            itemArr.push(item);
        });
        if(isRepeat(itemArr,'itemCode')||isRepeat(itemArr,'itemName')){
            febs.alert.warn("字典项目有重复项!");
            return false;
        }
        data.dictItems = itemArr;
        if (data.dictId && data.dictId.length > 0) {
            febs.modal.confirm('保存字典', '确定保存字典？', function () {
                febs.postArray(ctx + 'dict/update', data, function () {
                    febs.alert.success('修改成功');
                    // $(window.parent.document).find("#query").trigger("click");
                    // parent.layer.closeAll();
                    $("#febs-dict").find("#query").trigger("click");
                    layer.closeAll();
                });
            });
        } else {
            data.status = '1'
            febs.modal.confirm('保存字典', '确定保存字典？', function () {
                febs.postArray(ctx + 'dict/add', data, function () {
                    febs.alert.success('新增成功');
                    // $(window.parent.document).find("#query").trigger("click");
                    $("#febs-dict").find("#query").trigger("click");
                    layer.closeAll();
                });
            });

        }
    }

    function removeItem(_e) {
        $(_e).parent().parent().remove();
    }

    $view.find("#addItem").on('click',function () {
        var item = '<div class="layui-row layui-col-space10">\n' +
            '                    <div class=" layui-col-md5">\n' +
            '                        <label class="layui-form-label jhg-form-item-require">项目编码：</label>\n' +
            '                        <div class="layui-input-block">\n' +
            '                            <input type="text" autocomplete="off" class="layui-input"\n' +
            '                                   maxlength="20" placeholder="请输入项目编码">\n' +
            '                        </div>\n' +
            '                    </div>\n' +
            '                    <div class=" layui-col-md5">\n' +
            '                        <label class="layui-form-label jhg-form-item-require">项目名称：</label>\n' +
            '                        <div class="layui-input-block">\n' +
            '                            <input type="text" autocomplete="off" class="layui-input"\n' +
            '                                   maxlength="20" placeholder="请输入项目名称">\n' +
            '                        </div>\n' +
            '                    </div>\n' +
            '                    <div class=" layui-col-md1" style="text-align: center;">\n' +
            '                            <input type="checkbox" lay-skin="switch" lay-text="启用|禁用">' +
            '                    </div>\n' +
            '                    <div class=" layui-col-md1">\n' +
            '                        <div class="layui-input-block-html deleteItem"><em class="layui-icon febs-red" style="cursor: pointer;" title="移除">&#xe7f9;</em></div>\n' +
            '                    </div>\n' +
            '                </div>';
        $view.find("#itemContent").append(item);
        form.render();
    })

    $view.find("#itemContent").on('click','.deleteItem',function () {
        removeItem(this);
    })

    /**
     * 字典项目对象
     * @param _code
     * @param _name
     */
    function Item(_id,_dictCode,_itemCode,_name,_itemStatus,_sort) {
        this.itemId = _id;
        this.dictCode = _dictCode;
        this.itemCode = _itemCode
        this.itemName = _name;
        this.itemStatus = _itemStatus;
        this.sortNum = _sort;
    }

    function isRepeat(arr,field) {
        for (var i = 0; i < arr.length - 1; i++) {
            for (var j = i + 1; j < arr.length; j++) {
                if (arr[i][field] === arr[j][field]) {
                    return true;
                }
            }
        }
        return false;
    }
});